<?php
require_once 'pages/TMSListPage.inc';

class TMSCategorisedListPage extends TMSListPage {
/**
 * @var TCandyNestedListModel
 */	
	protected $_ioc_nav_model_;
	
	private $_tree_;
	
	public $FilterProperty;
	public $NodeIconType = 'folder';
	
	public function __get($nm){
		switch ($nm){
			case 'RootUrl':return $this->Url(null,null,array('node'=>''));
			case 'CreateLink':{
				if ($this->request->node && $this->_bool_allow_create_)
					return $this->service->ItemCreateUrl($this->Model->ClassName(),array('node'=>$this->request->node));
				return parent::__get($nm); 	
			}break;
			case 'CurrentNode':return $this->request->node;break; 
			case 'Navigation':{
				if (is_null($this->_tree_)){
					$this->_tree_ = new TMSPageItemIterator($this->NavModel->GetContents(null,false),$this);
				}
				return $this->_tree_;
			}break;
			default:return parent::__get($nm);break;
		}
	}
	
	protected function filterOptions(THttpRequest $request){
		$result = parent::filterOptions($request);
		if ($request->node)
			$result[] = new TFilterOption($this->FilterProperty, TConditionType::C_EQUAL, $request->node);	 
 		return $result;	
	}
	
	public function ItemOpenUrl(IItem $item){
		if ($item->ClassName() == $this->NavModel->ClassName())
			return $this->Url(null,null,array('node'=>$item->ItemId()));
		return parent::ItemOpenUrl($item);
	}
	
	protected function processUrlParameters(array $parameters){
		if (!isset($parameters['node']) && ($this->request instanceof THttpRequest))
			$parameters['node'] = $this->request->node;
		return $parameters;
	}

	public function Handle(THttpRequest $request){
		if ($request->node)
			$this->SetCookie($this->Name().'_node', $request->node);
		parent::Handle($request);
	}
	
}
